<?php

class SupplierCustomerInventoriesController extends AppController {

    var $name = 'SupplierCustomerInventories';
    var $uses = array('CustProductSite', 'Product', 'Customer', 'Site', 'Category');
    var $viewPath = 'suppliers/delivery/customer_inventories';
    var $pageTitle = 'Customer Inventory Status';
    var $paginate = array('CustProductSite' => array('order' => array('CustProductSite.stock_code')));

    function beforeFilter() {
        parent::beforeFilter();
        $this->set('parentNav', 'supplier_customer_inventories');
    }

    function index() {
        
    }

    function display($customer_id, $site_id = null, $supplier_id = null) {
        // ถ้าเข้าหน้า Display Customer นี้ครั้งแรก ยังไม่มี Site ที่กำลังดู ให้เป็น Site แรก ของ Customer นี้
        if ($site_id == null) {
            $s = $this->Site->find('first', array(
                        'fields' => array('id'),
                        'conditions' => array('customer_id' => $customer_id
                        )
                    ));
            $site_id = $s['Site']['id'];
        }

        // เก็บ Customer id ไว้ใน Session เอาไว้ใช้หน้า view ต่อ
        $this->Session->write('Auth.User.Extend.customer_id', $customer_id);

        $this->paginate['CustProductSite']['order'] = array('CustProductSite.stock_code ASC');
        $this->paginate['CustProductSite']['contain'] = array('Site', 'Product.Supplier', 'Product.Category', 'Product.Brand', 'Product.supplier_part_no', 'Product.part_no', 'Product.model', 'Product.title', 'Product.text');
        $this->data['CustProductSite']['customer_id'] = $customer_id;
        $this->data['CustProductSite']['site_id'] = $site_id;
        // ถ้าเป็น Vendor ดู ดึง supplier_id จาก session ถ้าไม่ใช่ (มาจากลูกค้า) ดึงจาก params
        $this->data['Product']['supplier_id'] = $supplier_id == null ? $this->Auth->user('supplier_id') : $supplier_id;
        parent::display('CustProductSite');

        $op = array(
            'stock_code' => 'LIKE',
            'part_no' => 'LIKE',
            'Product.category_id' => '=',
            'brand_id' => '=',
            'Product.model' => 'LIKE',
            'Product.title' => 'LIKE',
            'Product.text' => 'LIKE',
            'customer_id' => '=',
            'site_id' => '=',
            'supplier_current_stock' => !empty($this->data['Operand']['current_stock']) ? $this->data['Operand']['current_stock'] : '>',
            'min_stock' => !empty($this->data['Operand']['min_stock']) ? $this->data['Operand']['min_stock'] : '>',
            'max_stock' => !empty($this->data['Operand']['max_stock']) ? $this->data['Operand']['max_stock'] : '>',
        );
        $this->_paging('CustProductSite', $op, 'AND');

        $this->Customer->id = $customer_id;
        $this->pageTitle = $this->Customer->field('name') . ' Inventory Status';
        $this->set('customer_id', $customer_id);
        $this->set('site_id', $site_id);
        $this->set('supplier_id', $supplier_id);
    }

    function view($id) {
        $site_id = $this->Session->read('Auth.User.Extend.view_site_inventory');
        $customer_id = $this->Session->read('Auth.User.Extend.customer_id');
        $this->CustProductSite->contain(
                array('Product' => array('Brand', 'Category', 'Unit'), 'CyclePeriod', 'ShortestPeriod', 'MaintenancePeriod')
        );
        $this->data = $this->CustProductSite->findById($id);
        $this->CustProductSite->id = $id;
        $neighbors = $this->CustProductSite->find('neighbors', array(
                    'field' => 'stock_code', 'value' => $this->data['CustProductSite']['stock_code'],
                    'fields' => array('id', 'stock_code'),
                    'order' => array('stock_code ASC'),
                    'conditions' => array(
                        'CustProductSite.site_id' => $site_id,
                        'CustProductSite.customer_id' => $customer_id,
                        'Product.supplier_id' => $this->Auth->user('supplier_id')
                    )
                ));
        $this->set('neighbors', $neighbors);
        $this->set('customer_id', $customer_id);
    }

    function change_view_site($customer_id) {
        $this->Session->write('Auth.User.Extend.view_site_inventory', $this->data['view_site_inventory']);
        $this->redirect('supplier_customer_inventories/display/' . $customer_id);
    }

    function popup_search() {
        parent::popup_search();
        $this->set('categories', $this->Product->Category->find('list'));
        $this->set('brands', $this->Product->Brand->find('list', array('conditions' => array('supplier_id' => $this->Auth->user('supplier_id')))));
        $this->set('operands', array('<' => '<', '<=' => '<=', '=' => '=', '>' => '>', '>=' => '>='));
        $this->set('popupWidth', '400');
        $this->set('action', 'display/' . $this->Session->read('Auth.User.Extend.customer_id'));
    }

    function popup_view($customer_id, $site_id, $supplier_id = null) {
        $this->layout = 'popup';
        // clear session การ search จากครั้งก่อน
        $this->Session->delete('Product');
        $this->Session->delete('Products');
        $this->Session->delete("CustProductSite.paging.site_id");
        $this->Session->delete("CustProductSite.paging.customer_id");

        // มาจากหน้า vendor ดึงจาก session มาจากลูกค้าส่ง parameter เข้ามา
        if ($supplier_id == null) {
            $supplier_id = $this->Auth->user('supplier_id');
        }

        $this->set('popupTitle', 'Select a Product');
        $this->set('popupWidth', '$j(document).width() - 100');

        // id ที่จะถูกเลือกเป็น default
        $this->set('site_id', $site_id);
        $this->set('customer_id', $customer_id);
        $this->set('supplier_id', $supplier_id);

        $this->set('categories', $this->Category->listCategoryBySupplierId($supplier_id));
        $this->set('brands', $this->Product->Brand->find('list', array('conditions' => array('supplier_id' => $supplier_id))));
    }

    /**
     * ใช้ร่วมกับ popup_view เป็นส่วนตารางแสดงแถว customers
     */
    function popup_paginate($customer_id, $site_id, $supplier_id = null) {

        $this->display($customer_id, $site_id, $supplier_id);
    }

    function json_find_by_supplier_part_no_site_id() {
        $this->layout = 'ajax';
        // ค้นหาข้อมูลโดย supplier part no และ site_id
        $this->CustProductSite->contain(array('Product' => array('Unit'), 'Supplier'));
        $data = $this->CustProductSite->find('first', array(
                    'conditions' => array(
                        'OR' => array(
                            'Product.supplier_part_no' => $this->data['supplier_part_no'],
                            'CustProductSite.stock_code' => $this->data['supplier_part_no']
                        ),
                        'CustProductSite.site_id' => $this->data['site_id']
                    )
                        )
        );

        $this->set('data', $data);
        // view เก็บอยู่ใน element
        $this->viewPath = 'elements';
        $this->render('ajax/json');
    }

    function json_find_by_supplier_part_no_site_id_supplier_id() {
        $this->layout = 'ajax';
        // ค้นหาข้อมูลโดย supplier part no และ site_id
        $this->CustProductSite->contain(array('Product' => array('Unit'), 'Supplier'));
        $data = $this->CustProductSite->find('first', array(
                    'conditions' => array(
                        'OR' => array(
                            'Product.supplier_part_no' => $this->data['supplier_part_no'],
                            'CustProductSite.stock_code' => $this->data['supplier_part_no']
                        ),
                        'CustProductSite.site_id' => $this->data['site_id'],
                        'CustProductSite.supplier_id' => $this->data['supplier_id']
                    )
                        )
        );

        $this->set('data', $data);
        // view เก็บอยู่ใน element
        $this->viewPath = 'elements';
        $this->render('ajax/json');
    }

}

?>
